package com.shujing.controller;

import com.shujing.pojo.Category;
import com.shujing.pojo.Result;
import com.shujing.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈书憬
 * @version 1.0
 * 2024/4/8
 */
@Slf4j
@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 该接口用于新增文章分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public Result category(@RequestBody @Validated(Category.Add.class) Category category) {
        log.info("分类名称插入数据{}", category);
        categoryService.category(category);
        return Result.success();
    }

    /**
     * 文章分类列表
     * 该接口用于获取当前已登录用户创建的所有文章分类
     *
     * @return
     */
    @GetMapping
    public Result<List<Category>> listCategory() {
        List<Category> categories = categoryService.list();
        return Result.success(categories);
    }

    /**
     * 该接口用于根据ID获取文章分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Category> detail(Integer id) {
        Category category = categoryService.selectDetail(id);
        return Result.success(category);
    }

    /**
     * 更新文章分类
     *
     * @param category
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Category.update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 删除文章分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteCategory(Integer id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }
}
